<?php
/*
	Version   : 1.1
	Created   : 20081002
	Modified  : 20090927
	Author    : Pablo Ramirez
	co-Author :
	Descr     : Controlador "Containers"
	Changelog : 1.0 - Version Inicial
				1.1 - Control de Acceso
*/
class ContainersController extends AppController
{
	var $name       = 'Containers';
	var $uses       = array('Container','ContainerType','Owner');
	var $helpers    = array('Html','Form','Javascript','Ajax');
	var $scaffold;
	
	function index() {
		$this->checkSession('admin');
		$this->layout = 'maintenance';
		
		if (empty($this->data)) {
		
		$containers = $this->Container->findAll(null,'*','cod_codigo ASC');
			foreach($containers as $container) {
				$temp = array();
				foreach($container['Container'] as $key=>$value) {
					$temp[$key] = $value;
				}
				$containerArray[] = $temp;
			}
			$this->set('containers', $containerArray);
		} 
	}
	
	function getAllContainers() {
		$this->checkSession('admin');
		Configure::write('debug', '0');
		$containerArray = array();	//this will hold our data from the database.
		$this->layout = 'ajax'; //this tells the controller to use the Ajax layout instead of the default layout.
		//$count = $this->Container->findCount(); //counts the number of records in User.
		$containers = $this->Container->findAll(null,'*','cod_codigo ASC'); //gets all the User records and sorts them by last name.

		foreach($containers as $container) {
			$temp = array();
			foreach($container['Container'] as $key=>$value) {
				$temp[$key] = $value;
			}
			$containerArray[] = $temp;
		}
		
		//$this->set('total',$count);
		//$this->set('containers','{"total":'.$count.',"containers":'.json_encode($containerArray).'}');
		$this->set('containers',$containerArray);
	}
	
	function add($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		
		if (!empty($this->data)){
			if ($this->Container->save($this->data)){
				$this->redirect('/containers'); 
			}
		} else {
			$this->set('container_types', $this->ContainerType->generateList());
			$this->set('owners', $this->Owner->generateList());
			$this->set('id', $id);
		}
	}
	
	function reefer_attributes() {
		$this->checkSession('admin');
		//se deben llenar los combobox machine_type, machine_model, machine_gas
		//$this->render = 'reefer_attributes';
		$cot_codigo = $this->data['Container']['cot_codigo'];
		$cot_reefer = $this->ContainerType->find("cot_codigo = '$cot_codigo'",array("cot_reefer_flag"));
		
		$this->set('cot_reefer_flag',$cot_reefer['ContainerType']['cot_reefer_flag']);
		$this->render('reefer_attributes', 'ajax');
		
	}
	
	function edit($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		
		$this->Container->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->Container->read();
			$this->set('container_types', $this->ContainerType->generateList());
			$this->set('owners', $this->Owner->generateList());
			$this->set('id', $id);
		}else{
			if ($this->Container->save($this->data)){
				$this->redirect('/containers');
			} else {
				$this->Session->setFlash('Error al editar el Item');
			}
		}
	}
	
	function view($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->Container->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->Container->read();
			/*if ($this->data['ContainerType']['cot_reefer_flag'] = 'F') {
				$this->set('cot_reefer_flag', $id);
			}*/
			$this->set('container_types', $this->ContainerType->generateList());
			$this->set('owners', $this->Owner->generateList());
			$this->set('id', $id);
		}
	}
	
	function delete($id) {
		$this->checkSession('admin');
		if ($this->Container->del($id)) { 
			$this->set('error', '0');
		} else {
			$this->set('error','1');
		}
		$this->redirect('/containers');
	
	}
}
?>


